package LeetCode.month02.day42;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class No289 {
    public void gameOfLife(int[][] board) {
        int m = board.length;
        int n = board[0].length;
        List<int[]> res = new ArrayList<>();
        int[][] dir = {{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}};
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if(board[i][j]==0){
                    int count = 0;
                    for (int k = 0; k < 8; k++) {
                        int newr = i+dir[k][0];
                        int newc = j+dir[k][1];
                        if(newr<0||newr==m||newc<0||newc==n) continue;
                        if(board[newr][newc]==1){
                            count++;
                        }
                    }
                    if(count==3) res.add(new int[]{i,j});
                }else{
                    int count = 0;
                    for (int k = 0; k < 8; k++) {
                        int newr = i+dir[k][0];
                        int newc = j+dir[k][1];
                        if(newr<0||newr==m||newc<0||newc==n) continue;
                        if(board[newr][newc]==1){
                            count++;
                        }
                    }
                    if(count==3||count==2) res.add(new int[]{i,j});
                }
            }
        }
        for (int[] ints : board) {
            Arrays.fill(ints,0);
        }

        for (int[] re : res) {
            board[re[0]][re[1]] = 1;
        }
    }
}
